home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / uupc11ys.zip / LIB / CATCHER.C < prev    next >
C/C++ Source or Header  |  1992-11-27  |  5KB  |  128 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    c a t c h e r . c                                               */
  3. /*                                                                    */
  4. /*    Ctrl-Break handler for UUPC/extended                            */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*    Since C I/O functions are not safe inside signal routines,      */
  9. /*    the code uses conditionals to use system-level DOS and OS/2     */
  10. /*    services.  Another option is to set global flags and do any     */
  11. /*    I/O operations outside the signal handler.                      */
  12. /*--------------------------------------------------------------------*/
  13.  
  14. /*--------------------------------------------------------------------*/
  15. /*                        System include files                        */
  16. /*--------------------------------------------------------------------*/
  17.  
  18. #include <stdio.h>
  19. #include <signal.h>
  20. #include <process.h>
  21. #include <stdlib.h>
  22.  
  23. /*--------------------------------------------------------------------*/
  24. /*                    UUPC/extended include files                     */
  25. /*--------------------------------------------------------------------*/
  26.  
  27. #include "lib.h"
  28. #include "timestmp.h"
  29. #include "catcher.h"
  30. #include "safeio.h"
  31.  
  32. /*--------------------------------------------------------------------*/
  33. /*                          Global variables                          */
  34. /*--------------------------------------------------------------------*/
  35.  
  36. boolean terminate_processing = FALSE;
  37. boolean interactive_processing = TRUE;
  38. boolean norecovery = TRUE;
  39.  
  40. int panic_rc = 69;
  41.  
  42. #define INVALID_CHAR '*'
  43.  
  44. /*--------------------------------------------------------------------*/
  45. /*    c t r l c h a n d l e r                                         */
  46. /*                                                                    */
  47. /*    Handles SIGINT (CTRL+C) interrupt; from MicroSoft Programmer's  */
  48. /*    Workbench QuickHelp samples                                     */
  49. /*--------------------------------------------------------------------*/
  50.  
  51. void ctrlchandler( void )
  52. {
  53.     int ch = INVALID_CHAR;
  54.  
  55. /*--------------------------------------------------------------------*/
  56. /*                  Disallow CTRL+C during handler.                   */
  57. /*--------------------------------------------------------------------*/
  58.  
  59.     signal( SIGINT, SIG_IGN );
  60.  
  61. /*--------------------------------------------------------------------*/
  62. /*          Don't ask if the program doesn't think we should          */
  63. /*--------------------------------------------------------------------*/
  64.  
  65.     if ( ! interactive_processing )
  66.     {
  67.  
  68.       safeout( "\r\n" );
  69.       safeout( compilen );
  70.       panic_rc = 100;
  71.       terminate_processing = interactive_processing = TRUE;
  72.       safeout(": *** Termination in progress ***\r\n");
  73.       signal( SIGINT, ctrlchandler );
  74.       return;
  75.     }
  76.  
  77.     if ( terminate_processing )
  78.       safeout( "Termination already in progress ... answer Y to SCRAM program");
  79.  
  80. /*--------------------------------------------------------------------*/
  81. /*                   Ask user if he/she/it is sure                    */
  82. /*--------------------------------------------------------------------*/
  83.  
  84.    while ( ch == INVALID_CHAR )
  85.    {
  86.       safeout( "\r\n" );
  87.       safeout( compilen );
  88.       safeout( ": Abort processing? (Y/N) " );
  89.       safeflush();            /* Flush any queued characters         */
  90.       ch = safein();
  91.  
  92.       switch( ch )
  93.       {
  94.  
  95.          case 'y':
  96.          case 'Y':
  97.             if ( terminate_processing || norecovery )
  98.             {
  99.                safeout("\n\rProgram aborted.\r\n");
  100.                _exit(100);
  101.             }
  102.  
  103.             terminate_processing = TRUE;  /* Controlled shutdown  */
  104.             panic_rc = 100;
  105.             safeout("\n\r*** Termination in progress ***\r\n");
  106.             break;
  107.  
  108.         case 'N':
  109.         case 'n':
  110.            safeout("\r\nResuming execution\r\n");
  111.            break;
  112.  
  113.         default:
  114.            safeout(" -- Invalid response\a");
  115.            ch = INVALID_CHAR;
  116.            break;
  117.  
  118.       } /* switch  */
  119.    } /* for */
  120.  
  121. /*--------------------------------------------------------------------*/
  122. /*    The CTRL+C interrupt must be reset to our handler since by      */
  123. /*    default it is reset to the system handler.                      */
  124. /*--------------------------------------------------------------------*/
  125.  
  126.     signal( SIGINT, ctrlchandler );
  127. } /* catcher */
  128.